{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在浏览器中运行pygwalker，可以用于分享数据（自行上传）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import streamlit as st\n",
    "import pandas as pd\n",
    "import pygwalker as pyg\n",
    "from pygwalker.api.streamlit import StreamlitRenderer\n",
    "\n",
    "uploaded_file = st.file_uploader(\"Choose a CSV file\", type=\"csv\")\n",
    "\n",
    "if uploaded_file is not None:\n",
    "\tdf = pd.read_csv(uploaded_file)\n",
    "\tpyg_app = StreamlitRenderer(df)\n",
    "\tpyg_app.explorer()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 传入已保存的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import streamlit as st\n",
    "import pandas as pd\n",
    "from pygwalker.api.streamlit import StreamlitRenderer\n",
    "\n",
    "st.set_page_config(layout=\"wide\")\n",
    "df = pd.read_json(\"./pygwalker_spec_1726736800331.json\")\n",
    "\n",
    "pyg_app = StreamlitRenderer(df, spec=df)\n",
    "pyg_app.explorer()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可将数据保存为JSON格式，并在pygwalker中读取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pygwalker as pyg\n",
    "walker = pyg.walk(dataset=df, spec=\"./pygwalker_spec_1726736800331.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vis_spec = r\"\"\"{\"config\":[{\"config\":{\"defaultAggregated\":false,\"geoms\":[\"auto\"],\"coordSystem\":\"generic\",\"limit\":-1,\"timezoneDisplayOffset\":0},\"encodings\":{\"dimensions\":[{\"fid\":\"I\",\"name\":\"I\",\"basename\":\"I\",\"semanticType\":\"quantitative\",\"analyticType\":\"dimension\",\"offset\":0},{\"fid\":\"gw_paint_fid\",\"analyticType\":\"dimension\",\"name\":\"Paint Field\",\"semanticType\":\"nominal\",\"computed\":true,\"expression\":{\"op\":\"paint\",\"as\":\"gw_paint_fid\",\"params\":[{\"type\":\"newmap\",\"value\":{\"dict\":{\"1\":{\"name\":\"L_1\",\"color\":\"#4c78a8\"},\"2\":{\"name\":\"L_2\",\"color\":\"#f58518\"},\"3\":{\"name\":\"L_3\",\"color\":\"#e45756\"},\"4\":{\"name\":\"L_4\",\"color\":\"#72b7b2\"},\"5\":{\"name\":\"L_5\",\"color\":\"#54a24b\"},\"6\":{\"name\":\"L_6\",\"color\":\"#eeca3b\"},\"7\":{\"name\":\"L_7\",\"color\":\"#b279a2\"},\"8\":{\"name\":\"L_8\",\"color\":\"#ff9da6\"},\"9\":{\"name\":\"L_9\",\"color\":\"#9d755d\"},\"10\":{\"name\":\"L_10\",\"color\":\"#bab0ac\"}},\"facets\":[{\"map\":\"7c4xCgAxDAPBoP8/Olx5VZqAHJjp7dVaAAAAAAAAAAAAwHvyKed7A5LugDH90gB9fX19fX19/V6/M0BfX39EvzJAX39KvzFA/71+/ke55uIr4GwD\",\"dimensions\":[{\"domain\":{\"type\":\"quantitative\",\"value\":[0,100],\"width\":128},\"fid\":\"G\"},{\"domain\":{\"type\":\"quantitative\",\"value\":[0,9.96654],\"width\":128},\"fid\":\"A\"}],\"usedColor\":[1,2]}],\"usedColor\":[1,2]}}]}},{\"fid\":\"gw_mea_key_fid\",\"name\":\"Measure names\",\"analyticType\":\"dimension\",\"semanticType\":\"nominal\"}],\"measures\":[{\"fid\":\"A\",\"name\":\"A\",\"basename\":\"A\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"B\",\"name\":\"B\",\"basename\":\"B\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"C\",\"name\":\"C\",\"basename\":\"C\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"D\",\"name\":\"D\",\"basename\":\"D\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"E\",\"name\":\"E\",\"basename\":\"E\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"F\",\"name\":\"F\",\"basename\":\"F\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"G\",\"name\":\"G\",\"basename\":\"G\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"H\",\"name\":\"H\",\"basename\":\"H\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"J\",\"name\":\"J\",\"basename\":\"J\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"K\",\"name\":\"K\",\"basename\":\"K\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"L\",\"name\":\"L\",\"basename\":\"L\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"M\",\"name\":\"M\",\"basename\":\"M\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"N\",\"name\":\"N\",\"basename\":\"N\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0},{\"fid\":\"gw_count_fid\",\"name\":\"Row count\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"computed\":true,\"expression\":{\"op\":\"one\",\"params\":[],\"as\":\"gw_count_fid\"}},{\"fid\":\"gw_mea_val_fid\",\"name\":\"Measure values\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\"}],\"rows\":[{\"fid\":\"G\",\"name\":\"G\",\"basename\":\"G\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0}],\"columns\":[{\"fid\":\"A\",\"name\":\"A\",\"basename\":\"A\",\"analyticType\":\"measure\",\"semanticType\":\"quantitative\",\"aggName\":\"sum\",\"offset\":0}],\"color\":[{\"fid\":\"gw_paint_fid\",\"analyticType\":\"dimension\",\"name\":\"Paint Field\",\"semanticType\":\"nominal\",\"computed\":true,\"expression\":{\"op\":\"paint\",\"as\":\"gw_paint_fid\",\"params\":[{\"type\":\"newmap\",\"value\":{\"dict\":{\"1\":{\"name\":\"L_1\",\"color\":\"#4c78a8\"},\"2\":{\"name\":\"L_2\",\"color\":\"#f58518\"},\"3\":{\"name\":\"L_3\",\"color\":\"#e45756\"},\"4\":{\"name\":\"L_4\",\"color\":\"#72b7b2\"},\"5\":{\"name\":\"L_5\",\"color\":\"#54a24b\"},\"6\":{\"name\":\"L_6\",\"color\":\"#eeca3b\"},\"7\":{\"name\":\"L_7\",\"color\":\"#b279a2\"},\"8\":{\"name\":\"L_8\",\"color\":\"#ff9da6\"},\"9\":{\"name\":\"L_9\",\"color\":\"#9d755d\"},\"10\":{\"name\":\"L_10\",\"color\":\"#bab0ac\"}},\"facets\":[{\"map\":\"7c4xCgAxDAPBoP8/Olx5VZqAHJjp7dVaAAAAAAAAAAAAwHvyKed7A5LugDH90gB9fX19fX19/V6/M0BfX39EvzJAX39KvzFA/71+/ke55uIr4GwD\",\"dimensions\":[{\"domain\":{\"type\":\"quantitative\",\"value\":[0,100],\"width\":128},\"fid\":\"G\"},{\"domain\":{\"type\":\"quantitative\",\"value\":[0,9.96654],\"width\":128},\"fid\":\"A\"}],\"usedColor\":[1,2]}],\"usedColor\":[1,2]}}]}}],\"opacity\":[],\"size\":[],\"shape\":[],\"radius\":[],\"theta\":[],\"longitude\":[],\"latitude\":[],\"geoId\":[],\"details\":[],\"filters\":[],\"text\":[]},\"layout\":{\"showActions\":false,\"showTableSummary\":false,\"stack\":\"stack\",\"interactiveScale\":false,\"zeroScale\":true,\"size\":{\"mode\":\"auto\",\"width\":320,\"height\":200},\"format\":{},\"geoKey\":\"name\",\"resolve\":{\"x\":false,\"y\":false,\"color\":false,\"opacity\":false,\"shape\":false,\"size\":false}},\"visId\":\"gw_fgON\",\"name\":\"Chart 1\"}],\"chart_map\":{},\"workflow_list\":[{\"workflow\":[{\"type\":\"transform\",\"transform\":[{\"key\":\"gw_paint_fid\",\"expression\":{\"op\":\"paint\",\"as\":\"gw_paint_fid\",\"params\":[{\"type\":\"newmap\",\"value\":{\"facets\":[{\"dimensions\":[{\"domain\":{\"type\":\"quantitative\",\"value\":[0,100],\"width\":128},\"fid\":\"G\"},{\"domain\":{\"type\":\"quantitative\",\"value\":[0,9.96654],\"width\":128},\"fid\":\"A\"}],\"map\":\"7c4xCgAxDAPBoP8/Olx5VZqAHJjp7dVaAAAAAAAAAAAAwHvyKed7A5LugDH90gB9fX19fX19/V6/M0BfX39EvzJAX39KvzFA/71+/ke55uIr4GwD\"}],\"dict\":{\"1\":{\"name\":\"L_1\"},\"2\":{\"name\":\"L_2\"}}}}]}}]},{\"type\":\"view\",\"query\":[{\"op\":\"raw\",\"fields\":[\"gw_paint_fid\",\"A\",\"G\"]}]}]}],\"version\":\"0.4.9.8\"}\"\"\"\n",
    "pyg.walk(df, spec=vis_spec)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import pygwalker as pyg\n",
    "\n",
    "df = pd.read_csv('house.data', sep=' ')\n",
    "html_str = pyg.to_html(df)\n",
    "html_str\n",
    "# walker = pyg.walk(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
